package com.bawei.lookfor2204a.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

/**
 * 作者：赵亮
 * 时间：2024/10/15 12:07
 * 功能：基类Fragment
 */
abstract class BaseFragment<VM : ViewModel, VDB : ViewDataBinding>(private val layoutId: Int) :
    Fragment() {

    lateinit var binding: VDB       // 处理页面控件- 代替findViewById
    lateinit var viewModel: VM      // 调用网络，给页面提供数据

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = DataBindingUtil.inflate(inflater, layoutId, container, false)    // 实例化binding
        viewModel = ViewModelProvider(this).get(getViewModel())     // 实例化ViewModel

        initView()  // 初始化View

        initData()  // 初始化数据

        return binding.root
    }

    abstract fun initData()

    abstract fun initView()

    abstract fun getViewModel(): Class<VM>

    override fun onDestroy() {
        super.onDestroy()
        // 避免内存泄露
        binding.unbind()
    }
}